import { useLoading } from './useLoading'
import { isBoolean } from '@/utils'

interface SwitchProps {
  subject?: string
  field?: string
  enableApiKey: string
  disableApiKey: string
  enableValue?: boolean | string
  disableValue?: boolean | string
  switchLabel?: [string, string]
  withDialog?: boolean
  onEnabled?: () => void
  onSuccess?: () => void
  paramFn?: (row: any) => any
}

// boolean 类型的开关
export function useBaseSwitch({
  subject = '',
  field = '',
  enableApiKey,
  disableApiKey,
  onSuccess = () => {},
  actions: [enableLabel, disableLabel] = ['开启', '关闭'],
  paramFn = row => ({ uuid: row.uuid }),
}) {
  const loading = ref(false)
  let apikey
  function toggleVal(row) {
    row[field] = !row[field]
  }
  async function toggle(row) {
    const tvalue = row[field] // 将要变成的值
    apikey = tvalue ? enableApiKey : disableApiKey

    try {
      loading.value = true
      const param = paramFn(row)
      const { success } = await useRequest(apikey, param)
      const action = tvalue ? enableLabel : disableLabel
      if (success) {
        onSuccess()
        ElNotification.success(`${subject}${action}成功`)
      }
      else { toggleVal(row) }
    }
    catch {
      toggleVal(row)
    }
    finally {
      loading.value = false
    }
  }

  return { loading, toggle, toggleVal }
}

/* 带确认框的开关  */
export function useConfirmSwitch(options) {
  const { loading, toggle: _toggle, toggleVal: reset } = useBaseSwitch(options)
  const { field, actions: [enableLabel = '开启', disableLabel = '关闭'] = [], subject, disableTip } = options
  const fullLoading = useLoading()
  function toggle(row) {
    const action = row[field] ? enableLabel : disableLabel
    const value = row[field]
    const message = value ? `${action}${subject}` : (disableTip || `${action}${subject}`)
    ElMessageBox.confirm(message, '提示', { type: 'warning' })
      .then(async () => {
        fullLoading.open()
        await _toggle(row)
        fullLoading.close()
      })
      .catch(() => {
        if (field)
          reset(row)
      }).finally(() => {
        fullLoading.close()
      })
  }

  return { loading, toggle }
}

/* 开启时带弹窗填写附加信息的开关 */
export function useSwitch(
  {
    subject = '',
    field = '',
    enableApiKey,
    disableApiKey,
    switchLabel: [enableLabel, disableLabel] = ['开启', '关闭'],
    enableValue = true,
    disableValue = false,
    withDialog = false,
    onEnabled = null,
    onSuccess = null,
    paramFn = row => ({ uuid: row.uuid }),
  }: SwitchProps,
) {
/** 切换 */
  const loading = ref(false)
  const toggleVisible = ref(false)
  const currentRow = ref<any>({})
  let apikey
  const fullLoading = useLoading()
  function reset() {
    if (isBoolean(currentRow.value[field]))
      currentRow.value[field] = !currentRow.value[field]
    else
      currentRow.value[field] = currentRow.value[field] === enableValue ? disableValue : enableValue
  }

  async function toggle(row) {
    const tvalue = row[field]
    currentRow.value = row
    apikey = tvalue === enableValue ? enableApiKey : disableApiKey

    /* 开启需要额外配置 */
    if (tvalue === enableValue && withDialog) {
      toggleVisible.value = true
      return
    }

    try {
      loading.value = true
      const param = paramFn(row)
      const { success } = await useRequest(apikey, param)
      loading.value = false
      const actiion = tvalue === enableValue ? enableLabel : disableLabel
      if (success) {
        onSuccess && onSuccess()
        ElNotification.success(`${subject}${actiion}成功`)
      }
      else { reset() }
    }
    catch {
      reset()
    }
  }

  function cancelToggle() {
    reset()
    toggleVisible.value = false
  }
  async function enableDone(params) {
    const uuidParam = paramFn(currentRow.value)
    fullLoading.open()
    const { success } = await useRequest(apikey, { ...uuidParam, ...params, [field]: true })
    onEnabled && onEnabled()
    fullLoading.close()
    if (success) {
      toggleVisible.value = false
      ElNotification.success(`${subject}开启成功`)
    }
    else { reset() }
  }
  return { loading, toggle, toggleVisible, cancelToggle, enableDone }
}
